<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.xpy.utils.ConnSqlServer" %>

<html>
<head>
    <title>新增管理员</title>
    <link rel="stylesheet" href="css/style2.css">
    <style>
        .form-container {
            width: 30%;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 10px;
            box-shadow: 2px 2px 12px #aaa;
        }
        .form-container h2 {
            text-align: center;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: inline-block;
            width: 40%;
            font-weight: bold;
            text-align: right;
            margin-right: 10px;
        }
        .form-group input, .form-group select {
            width: 50%;
            padding: 5px;
            box-sizing: border-box;
        }
        .form-buttons {
            text-align: center;
            margin-top: 20px;
        }
        .form-buttons button {
            margin: 0 10px;
            padding: 5px 15px;
        }
    </style>
</head>
<body>
<div class="form-container">
    <h2>新增管理员</h2>
    <form action="admin_new.jsp" method="post"> <!-- 表单提交到自身 -->
        <div class="form-group">
            <label for="name">管理员姓名：</label>
            <input type="text" id="name" name="name" required>
        </div>
        <div class="form-group">
            <label for="password">密码：</label>
            <input type="password" id="password" name="password" required>
        </div>
        <div class="form-group">
            <label for="confirmPassword">确认密码：</label>
            <input type="password" id="confirmPassword" name="confirmPassword" required>
        </div>
        <div class="form-group">
            <label for="power">权限：</label>
            <select id="power" name="power">
                <option value="1">超级管理员</option>
                <option value="0" selected>普通管理员</option>
            </select>
        </div>
        <div class="form-buttons">
            <button type="submit">确定</button>
            <button type="reset">取消</button>
            <button type="button" onclick="window.location.href='admin_admin.jsp'">返回</button>
        </div>
    </form>
</div>

<%
    if ("POST".equalsIgnoreCase(request.getMethod())) {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String confirmPassword = request.getParameter("confirmPassword");
        String powerStr = request.getParameter("power");
        int power = Integer.parseInt(powerStr); // 将字符串转换为整数

        // 密码匹配检查
        if (password.equals(confirmPassword)) {
            Connection conn = null;
            PreparedStatement pstm = null;
            try {
                conn = ConnSqlServer.getConnection();
                // 准备SQL插入语句，注意id字段不需要插入
                String sql = "INSERT INTO admin (name, pwd, power) VALUES (?, ?, ?)";
                pstm = conn.prepareStatement(sql);
                pstm.setString(1, name);
                pstm.setString(2, password);
                pstm.setInt(3, power);
                int result = pstm.executeUpdate();
                if (result > 0) {
                    // 插入成功
                    out.println("<script>alert('管理员添加成功！'); window.location.href='admin_admin.jsp';</script>");
                } else {
                    // 插入失败
                    out.println("<script>alert('添加管理员失败，请重试。'); window.history.back();</script>");
                }
            } catch (SQLException e) {
                // 打印异常信息到服务器日志
                e.printStackTrace();
                out.println("<script>alert('发生错误：' + " + e.getMessage() + ");</script>");
            } finally {
                // 关闭PreparedStatement和Connection
                if (pstm != null) {
                    try {
                        pstm.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            // 密码不匹配
            out.println("<script>alert('密码和确认密码不匹配，请重新输入！'); window.history.back();</script>");
        }
    }
%>
</body>
</html>